const BaseController = require('./base');

class ExamFrontController extends BaseController {
  async me() {
    const { ctx } = this;
    const { key } = ctx.query;
    const res = await ctx.service.examfront.me(key);
    if (res.state) {
      return this.success(res.data);
    }
    return this.fail('参数错误');
  }

  async getToken() {
    const { ctx } = this;
    ctx.validate({
      email: { type: 'email' },
      userId: { type: 'string' },
    });
    const { email, userId } = ctx.request.body;
    const { key } = ctx.query;
    const res = await ctx.service.examfront.getToken(email, userId, key);
    if (res.state) {
      return this.success(res.data);
    }
    return this.fail('参数错误');
  }

  async getExam() {
    const { ctx } = this;
    const token = ctx.request.header.authorization?.slice(7);
    const userInfo = this.app.jwt.decode(token);
    const { examId } = userInfo;
    if (!examId) {
      return this.fail('参数错误');
    }
    const exam = await ctx.service.examfront.getExam(examId);
    if (exam) {
      return this.success(exam);
    }
    return this.fail('笔试不存在');
  }

  async uploadSignature() {
    const { ctx } = this;
    ctx.validate({
      signature: { type: 'string' },
    });
    const { signature } = ctx.request.body;
    const res = await ctx.service.examfront.uploadSignature(signature);
    if (res) {
      return this.success('上传成功');
    }
    return this.fail('参数错误');
  }

  async getExamPaper() {
    const { ctx } = this;
    const token = ctx.request.header.authorization?.slice(7);
    const userInfo = this.app.jwt.decode(token);
    const { examId } = userInfo;
    if (!examId) {
      return this.fail('参数错误');
    }
    const res = await ctx.service.examfront.getExamPaper(userInfo.id, examId);
    const exam = await ctx.service.examfront.getExam(examId);
    if (res.state && exam) {
      return this.success({
        examPaper: res.data,
        exam,
      });
    }
    return this.fail('试卷不存在');
  }

  async begin() {
    const { ctx } = this;
    const token = ctx.request.header.authorization?.slice(7);
    const userInfo = this.app.jwt.decode(token);
    const { id, examId } = userInfo;
    if (!examId) {
      return this.fail('参数错误');
    }
    const begin = await ctx.service.examfront.begin(id, examId);
    if (begin.state) {
      return this.success({ code: 200 });
    }
    return this.fail(begin.msg);
  }

  async checkConfig() {
    const { ctx } = this;
    const token = ctx.request.header.authorization?.slice(7);
    const userInfo = this.app.jwt.decode(token);
    const { id, examId } = userInfo;
    if (!examId) {
      return this.fail('参数错误');
    }
    const exam = await ctx.service.examfront.checkConfig(id, examId);
    if (exam) {
      return this.success({
        exam,
      });
    }
    return this.fail('试卷不存在');
  }

  async submitExamPaper() {
    const { ctx } = this;
    ctx.validate({
      examPaper: { type: 'content' },
    });
    const { examPaper } = ctx.request.body;
    const token = ctx.request.header.authorization?.slice(7);
    const userInfo = this.app.jwt.decode(token);
    const { id, examId } = userInfo;
    const res = await ctx.service.examfront.submitExamPaper(examPaper, id, examId);
    if (res.state) {
      return this.success({ code: 200 });
    }
    return this.fail(res.msg);
  }

  async sendJumpMsg() {
    const { ctx } = this;
    const token = ctx.request.header.authorization?.slice(7);
    const userInfo = this.app.jwt.decode(token);
    const { id, examId } = userInfo;
    const res = await ctx.service.examfront.sendJumpMsg(id, examId);
    if (res) {
      return this.success({ code: 200 });
    }
    return this.fail('发送失败');
  }
}

module.exports = ExamFrontController;
